﻿namespace Cosby
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Hosting;
    using System.Web.Mvc;
    using Cosby.Models;

    public class ImageController : Controller
    {
        public FileResult Gradient(string orientation, int length, string startColor, string endColor)
        {
            LinearGradientMode mode = StripeSet.GetMode(orientation);
            var fullyQualifiedPath = getFileName(mode, length, startColor, endColor);

            if (!System.IO.File.Exists(fullyQualifiedPath))
            {
                //Create
                CreateGradient(fullyQualifiedPath, mode, length, startColor, endColor);
            }

            return base.File(fullyQualifiedPath, "image/png");
        }

        
        // GET: /Image/Stripes/50/FFF/2/FEE/1/000/2/FFE  
        //  Parameters are 'width' then 'color' over and over, as many as you want
        public FileResult Stripes(string orientation, string widthcolors)
        {
            var ss = new StripeSet(orientation, widthcolors);

            var fullyQualifiedPath = getFileName(ss.Mode, ss.Stripes);
            if (!System.IO.File.Exists(fullyQualifiedPath))
            {
                //Create
                CreateStripes(fullyQualifiedPath, ss.Mode, ss.Stripes);
            }

            return base.File(fullyQualifiedPath, "image/png");
        }

        
        
        private void CreateStripes(string fullyQualifiedPath, LinearGradientMode mode, List<Stripe> stripes)
        {
            var bitMap = new Bitmap(stripes.Sum(f => f.Width),1);//  width * colorList.Count, 1);
            using (var g = Graphics.FromImage(bitMap))
            {
                int widthSoFar = 0;
                foreach (var stripe in stripes)
                {
                    var brush = new SolidBrush(stripe.Color);
                    var rect = new Rectangle(widthSoFar, 0, stripe.Width, 1);
                    g.FillRectangle(brush, rect);
                    widthSoFar += stripe.Width;
                }
                g.RotateTransform(90);
                if (mode == LinearGradientMode.Horizontal)
                    bitMap.RotateFlip(RotateFlipType.Rotate90FlipNone); 
                bitMap.Save(fullyQualifiedPath, ImageFormat.Png);
            }
        }

        private void CreateGradient(string fullyQualifiedPath, LinearGradientMode mode, int length, string startColor, string endColor)
        {
            int height = (mode == LinearGradientMode.Vertical) ? length : 1;
            length = !(mode == LinearGradientMode.Vertical) ? length : 1;
            var color1 = StripeSet.GetColor(startColor);// System.Drawing.Color.Green; //TODO: Use startColor
            var color2 = StripeSet.GetColor(endColor);// System.Drawing.Color.DarkBlue; //TODO: Use endColor
            var bitMapgradient = new Bitmap(length, height);
            var brushSize = new Rectangle(0, 0, length, height);
            using (var g = Graphics.FromImage(bitMapgradient))
            {
                var lgb = new LinearGradientBrush(brushSize, color1, color2, mode);
                g.FillRectangle(lgb, brushSize);
                bitMapgradient.Save(fullyQualifiedPath, ImageFormat.Png);
            }
        }

        private string getFileName(LinearGradientMode mode, List<Stripe> stripes)
        {
            string fileName = mode.ToString()[0].ToString() + "_";
            stripes.ForEach(p => fileName += p.Width.ToString() + "_" + p.ColorString + "_");
            return getFileName(fileName + ".PNG");
        }

        private static string getFileName(LinearGradientMode mode, int length, string startColor, string endColor)
        {
            string fileName = mode.ToString()[0].ToString() + "_" + length.ToString() + "_" + startColor + "_" + endColor + ".PNG";
            return getFileName(fileName);
        }

        private static string getFileName(string fileName)
        {
            var path = "~/Content/images/";

            if (HostingEnvironment.ApplicationPhysicalPath.Length + path.Length + fileName.Length >  250)
            {
                fileName = fileName.GetHashCode().ToString();
            }

            var fullyQualifiedPath = Path.Combine(VirtualPathUtility.Combine(VirtualPathUtility.AppendTrailingSlash(HttpRuntime.AppDomainAppVirtualPath), path), fileName);

            
            fullyQualifiedPath = HostingEnvironment.MapPath(fullyQualifiedPath);
            return fullyQualifiedPath;
        }

     
    }
}
namespace ASP
{
    public class _Page_Views_Home_Index_cshtml
    {
        public _Page_Views_Home_Index_cshtml()
        {
            System.Diagnostics.Debug.WriteLine("Hello");
        }
    }
}

